package com.SmartCare.controller.client;

import com.SmartCare.common.Result;
import com.SmartCare.common.vo.role.ElderlyInformationVo;
import com.SmartCare.pojo.role.ElderlyInformation;
import com.SmartCare.service.role.ElderlyInformationService;
import com.SmartCare.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/client")
@Api(tags = "C端老人列表")
@Slf4j
public class ElderController {
    @Resource
    private ElderlyInformationService elderService;

    @Resource
    ImageService imageService;
    @Value("${files.upload.path}/elderlyImg/")
    private String fileUploadPath;

    /**
     * 根据名称获取老人列表
     */
    @GetMapping("/elder/query")
    public Result getElderList(@Parameter(description = "护工ID", required = true) @RequestParam Long workerId,
                               @RequestParam(required = false) String content){
        return elderService.getElderList(workerId,content);
    }


    /**
     * 添加老人图片
     */
    @PostMapping("/elder/upload")
    @Operation(summary = "上传图片文件", description = "将返回该图片的下载地址")
    public Result addImage(@RequestParam List<MultipartFile> file) {
        return imageService.upload(file, fileUploadPath, "client/elder");
    }


    /**
     * 下载图片
     */
    @GetMapping("/elder/{fileUuid}")
    @Operation(summary = "下载图片文件", description = "直接访问该路径可下载图片")
    public void download(@PathVariable String fileUuid, HttpServletResponse response) throws IOException {
        imageService.download(fileUuid, fileUploadPath, response);
    }

    /**
     * 更新老人信息
     */
    @PutMapping("/elder/update")
    public Result updateElderInformation(@RequestBody ElderlyInformationVo elderlyInformationVo){
        ElderlyInformation information = new ElderlyInformation();
        BeanUtils.copyProperties(elderlyInformationVo,information);
        return elderService.updateElder(information);
    }

    /**
     * 获取老人信息
     */
    @GetMapping("/elder/getMessage")
    public Result getElderById(@RequestParam Long elderId){
        return elderService.getElderData(elderId);
    }

    /**
     * 老人模块获取个人信息
     */
    @GetMapping("/elder/info")
    public Result gerElderInfo(Long userId){
        return elderService.gerElderInfo(userId);
    }

    /**
     * 更新老人信息
     */
    @GetMapping("/elder/newInfo")
    public Result updateInfo(@RequestParam Long elderId,
                             @RequestParam(required = false) String name,
                             @RequestParam(required = false) Integer sex,
                             @RequestParam(required = false) Integer age,
                             @RequestParam(required = false) String imageUrl,
                             @RequestParam(required = false) String iconUrl){
        return elderService.updateInfo(elderId,name,sex,age,imageUrl,iconUrl);
    }

}
